package Gui;


import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import net.miginfocom.swing.MigLayout;


@SuppressWarnings("serial")
public class lettersPanel extends JPanel {

	static letterButton btnA;
	static letterButton btnB;
	static letterButton btnC;
	static letterButton btnD;
	static letterButton btnE;
	static letterButton btnF;
	static letterButton btnG;
	static letterButton btnH;
	static letterButton btnI;
	static letterButton btnJ;
	static letterButton btnK;
	static letterButton btnL;
	static letterButton btnM;
	static letterButton btnN;
	static letterButton btnO;
	static letterButton btnP;
	static letterButton btnQ;
	static letterButton btnR;
	static letterButton btnS;
	static letterButton btnT;
	static letterButton btnU;
	static letterButton btnV;
	static letterButton btnW;
	static letterButton btnX;
	static letterButton btnY;
	static letterButton btnZ;
	char letter;
	static Player whopressed;
	private static JButton btnEnter;
	static JButton btnContinue;
	private JButton btnHint;
	private String hint="";


	/**
	 * Create the panel.
	 */
	class letterButton extends JButton{
		public boolean pressed;
		public letterButton(String str){
			super.setText(str);
			this.pressed=false;
		}

		public boolean isPressed(){
			return pressed;
		}
		public void setPressed(boolean bool){
			pressed=bool;
		}
	}


	public lettersPanel() {
		setEnabled(false);
		setLayout(new MigLayout("", "[67.00px][59.00px][69.00px][54.00px][65.00px,grow][63.00px][54px][54px][54px][54px]", "[64px][64px][64px][62.00][]"));

		ActionListener al=new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				disableAll();
				btnContinue.setEnabled(true);
				whopressed.getLet(((arg0.getActionCommand().charAt(0))));
				letterButton lb=(letterButton) arg0.getSource();
				lb.setPressed(true);
			}
		};

		ActionListener vowels=new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				int choice =JOptionPane.showConfirmDialog((letterButton)arg0.getSource(), "Would you like to buy this vowel for 50$?","Buy a Vowel",2);
				if (choice==0){
					if (whopressed.getScore()>50){
						disableAll();
						btnContinue.setEnabled(true);
						whopressed.getVowel(((arg0.getActionCommand().charAt(0))));
						letterButton lb=(letterButton) arg0.getSource();
						lb.setPressed(true);}
					else JOptionPane.showMessageDialog((letterButton)arg0.getSource(),"not enough money");}
				else{

				}
			}
		};

		btnQ = new letterButton("Q");
		btnQ.setEnabled(false);
		btnQ.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(btnQ, "cell 0 0,grow");

		btnW = new letterButton("W");
		btnW.setEnabled(false);
		btnW.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(btnW, "cell 1 0,grow");

		btnE = new letterButton("E");
		btnE.setEnabled(false);
		add(btnE, "cell 2 0,grow");

		btnR = new letterButton("R");
		btnR.setEnabled(false);
		add(btnR, "cell 3 0,grow");

		btnT = new letterButton("T");
		btnT.setEnabled(false);
		add(btnT, "cell 4 0,grow");

		btnY = new letterButton("Y");
		btnY.setEnabled(false);
		add(btnY, "cell 5 0,grow");

		btnU = new letterButton("U");
		btnU.setEnabled(false);
		add(btnU, "cell 6 0,grow");

		btnI = new letterButton("I");
		btnI.setEnabled(false);
		add(btnI, "cell 7 0,grow");

		btnO = new letterButton("O");
		btnO.setEnabled(false);
		add(btnO, "cell 8 0,grow");

		btnP = new letterButton("P");
		btnP.setEnabled(false);
		add(btnP, "cell 9 0,grow");

		btnA = new letterButton("A");
		btnA.setEnabled(false);
		add(btnA, "cell 1 1,grow");

		btnS = new letterButton("S");
		btnS.setEnabled(false);
		add(btnS, "cell 2 1,grow");

		btnD = new letterButton("D");
		btnD.setEnabled(false);
		add(btnD, "cell 3 1,grow");

		btnF = new letterButton("F");
		btnF.setEnabled(false);
		add(btnF, "cell 4 1,grow");

		btnG = new letterButton("G");
		btnG.setEnabled(false);
		add(btnG, "cell 5 1,grow");

		btnH = new letterButton("H");
		btnH.setEnabled(false);
		add(btnH, "cell 6 1,grow");

		btnJ = new letterButton("J");
		btnJ.setEnabled(false);
		add(btnJ, "cell 7 1,grow");

		btnK = new letterButton("K");
		btnK.setEnabled(false);
		add(btnK, "cell 8 1,grow");

		btnL = new letterButton("L");
		btnL.setEnabled(false);
		add(btnL, "cell 9 1,grow");

		btnZ = new letterButton("Z");
		btnZ.setEnabled(false);
		add(btnZ, "cell 2 2,grow");

		btnX = new letterButton("X");
		btnX.setEnabled(false);
		add(btnX, "cell 3 2,grow");

		btnC = new letterButton("C");
		btnC.setEnabled(false);
		add(btnC, "cell 4 2,grow");

		btnV = new letterButton("V");
		btnV.setEnabled(false);
		add(btnV, "cell 5 2,grow");

		btnB = new letterButton("B");
		btnB.setEnabled(false);
		add(btnB, "cell 6 2,grow");

		btnN = new letterButton("N");
		btnN.setEnabled(false);
		add(btnN, "cell 7 2,grow");

		btnM = new letterButton("M");
		btnM.setEnabled(false);
		add(btnM, "cell 8 2,grow");

		btnContinue =  new JButton("End turn");
		btnContinue.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnContinue.setEnabled(false);
				whopressed.endTurn();
			}
		});

		
		
		btnEnter = new JButton("Guess");
		btnEnter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GuessFrame guessframe = new GuessFrame();
				guessframe.setVisible(true);
				btnContinue.doClick();
			}
		});
		
		btnHint = new JButton("Get Hint");
		add(btnHint, "cell 2 4");
		add(btnEnter, "cell 3 4");
		add(btnContinue, "cell 4 4 2 1,growx");
		btnHint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int choice =JOptionPane.showConfirmDialog(null, "Would you like to buy a hint for 200$?","Buy a hint?",2);
				if (choice==0){
					whopressed.setScore(whopressed.score - 200);
					HintFrame hintframe = new HintFrame(hint);
					hintframe.setVisible(true);
				}
			}});

		btnA.addActionListener(vowels);
		btnB.addActionListener(al);
		btnC.addActionListener(al);
		btnD.addActionListener(al);
		btnE.addActionListener(vowels);
		btnF.addActionListener(al);
		btnG.addActionListener(al);
		btnH.addActionListener(al);
		btnI.addActionListener(vowels);
		btnJ.addActionListener(al);
		btnK.addActionListener(al);
		btnL.addActionListener(al);
		btnM.addActionListener(al);
		btnN.addActionListener(al);
		btnO.addActionListener(vowels);
		btnP.addActionListener(al);
		btnQ.addActionListener(al);
		btnR.addActionListener(al);
		btnS.addActionListener(al);
		btnT.addActionListener(al);
		btnU.addActionListener(vowels);
		btnV.addActionListener(al);
		btnW.addActionListener(al);
		btnX.addActionListener(al);
		btnY.addActionListener(al);
		btnZ.addActionListener(al);
	}


	public void enableAll(Player player){
		whopressed=player;
		if (!btnA.isPressed())
			btnA.setEnabled(true);
		if (!btnB.isPressed())
			btnB.setEnabled(true);
		if (!btnC.isPressed())
			btnC.setEnabled(true);
		if (!btnD.isPressed())
			btnD.setEnabled(true);
		if (!btnE.isPressed())
			btnE.setEnabled(true);
		if (!btnF.isPressed())
			btnF.setEnabled(true);
		if (!btnG.isPressed())
			btnG.setEnabled(true);
		if (!btnH.isPressed())
			btnH.setEnabled(true);
		if (!btnI.isPressed())
			btnI.setEnabled(true);
		if (!btnJ.isPressed())
			btnJ.setEnabled(true);
		if (!btnK.isPressed())
			btnK.setEnabled(true);
		if (!btnL.isPressed())
			btnL.setEnabled(true);
		if (!btnM.isPressed())
			btnM.setEnabled(true);
		if (!btnN.isPressed())
			btnN.setEnabled(true);
		if (!btnO.isPressed())
			btnO.setEnabled(true);
		if (!btnP.isPressed())
			btnP.setEnabled(true);
		if (!btnQ.isPressed())
			btnQ.setEnabled(true);
		if (!btnR.isPressed())
			btnR.setEnabled(true);
		if (!btnS.isPressed())
			btnS.setEnabled(true);
		if (!btnT.isPressed())
			btnT.setEnabled(true);
		if (!btnU.isPressed())
			btnU.setEnabled(true);
		if (!btnV.isPressed())
			btnV.setEnabled(true);
		if (!btnW.isPressed())
			btnW.setEnabled(true);
		if (!btnX.isPressed())
			btnX.setEnabled(true);
		if (!btnY.isPressed())
			btnY.setEnabled(true);
		if (!btnZ.isPressed())
			btnZ.setEnabled(true);

	}


	static public void disableAll(){
		btnA.setEnabled(false);
		btnB.setEnabled(false);
		btnC.setEnabled(false);
		btnD.setEnabled(false);
		btnE.setEnabled(false);
		btnF.setEnabled(false);
		btnG.setEnabled(false);
		btnH.setEnabled(false);
		btnI.setEnabled(false);
		btnJ.setEnabled(false);
		btnK.setEnabled(false);
		btnL.setEnabled(false);
		btnM.setEnabled(false);
		btnN.setEnabled(false);
		btnO.setEnabled(false);
		btnP.setEnabled(false);
		btnQ.setEnabled(false);
		btnR.setEnabled(false);
		btnS.setEnabled(false);
		btnT.setEnabled(false);
		btnU.setEnabled(false);
		btnV.setEnabled(false);
		btnW.setEnabled(false);
		btnX.setEnabled(false);
		btnY.setEnabled(false);
		btnZ.setEnabled(false);

	}


	public void resetPressedflag() {
		btnA.setPressed(false);
		btnB.setPressed(false);
		btnC.setPressed(false);
		btnD.setPressed(false);
		btnE.setPressed(false);
		btnF.setPressed(false);
		btnG.setPressed(false);
		btnH.setPressed(false);
		btnI.setPressed(false);
		btnJ.setPressed(false);
		btnK.setPressed(false);
		btnL.setPressed(false);
		btnM.setPressed(false);
		btnN.setPressed(false);
		btnO.setPressed(false);
		btnP.setPressed(false);
		btnQ.setPressed(false);
		btnR.setPressed(false);
		btnS.setPressed(false);
		btnT.setPressed(false);
		btnU.setPressed(false);
		btnV.setPressed(false);
		btnW.setPressed(false);
		btnX.setPressed(false);
		btnY.setPressed(false);
		btnZ.setPressed(false);

	}


	@SuppressWarnings("unused")
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});

	}

	public void setPressedChar(char c){
		if (c == 'A')
			btnA.setPressed(true);
		if (c == 'B')
			btnB.setPressed(true);
		if (c == 'C')
			btnC.setPressed(true);
		if (c == 'D')
			btnD.setPressed(true);
		if (c == 'E')
			btnE.setPressed(true);
		if (c == 'F')
			btnF.setPressed(true);
		if (c == 'G')
			btnG.setPressed(true);
		if (c == 'H')
			btnH.setPressed(true);
		if (c == 'I')
			btnI.setPressed(true);
		if (c == 'J')
			btnJ.setPressed(true);
		if (c == 'K')
			btnK.setPressed(true);
		if (c == 'L')
			btnL.setPressed(true);
		if (c == 'M')
			btnM.setPressed(true);
		if (c == 'N')
			btnN.setPressed(true);
		if (c == 'O')
			btnO.setPressed(true);
		if (c == 'P')
			btnP.setPressed(true);
		if (c == 'Q')
			btnQ.setPressed(true);
		if (c == 'R')
			btnR.setPressed(true);
		if (c == 'S')
			btnS.setPressed(true);
		if (c == 'T')
			btnT.setPressed(true);
		if (c == 'U')
			btnU.setPressed(true);
		if (c == 'V')
			btnV.setPressed(true);
		if (c == 'W')
			btnW.setPressed(true);
		if (c == 'X')
			btnX.setPressed(true);
		if (c == 'Y')
			btnY.setPressed(true);
		if (c == 'Z')
			btnZ.setPressed(true);
	}

	public void updateHintButton(){
		hint = OfflineGame.round.getPhrase().getHint();
		if (hint.isEmpty()){
			btnHint.setEnabled(false);
		}
		else {
			btnHint.setEnabled(true);
		}
	}
	

	public static void onGuess(){
		disableAll();
		btnContinue.setEnabled(true);
		btnEnter.setEnabled(true);
	}


}

